package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 修改套餐
     * 
 * @param setmealDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/25
     **/
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 套餐分页查询
     * 
 * @param setmealPageQueryDTO
     * @return 
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @Override
    public PageResult pageSetmealQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmeals = setmealMapper.pageSetmealQuery(setmealPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmeals.getTotal());
        pageResult.setRecords(setmeals.getResult());
        return pageResult;
    }

    /**
     * 套餐起售禁售
     *
 * @param status
 * @param id
     * @return
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @Override
    public void startorStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 删除套餐
     * 
 * @param ids
     * @return 
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @Override
    public void deleteSetmeal(ArrayList<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(id -> {
            setmealMapper.deleteSetmealDish(id);
        });
        setmealMapper.batchDeleteSetmeal(ids);
    }

    /**
     * 新增套餐
     * 
 * @param setmealDTO
     * @return 
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertBathSetmealDish(setmealDTO.getSetmealDishes());
        setmealMapper.insertSetmeal(setmeal);
    }

    /**
     * 根据id查询套餐
     *
 * @param id
     * @return
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @Override
    public SetmealVO getSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        List<SetmealDish> setmealDishById = setmealMapper.getSetmealDishById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishById);
        return setmealVO;
    }

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
}
